package cn.codenest.listener;

import com.rabbitmq.client.ConfirmListener;

import java.io.IOException;
import java.util.Map;

/**
 * @author ：Hyman
 * @date ：Created in 2021/7/13 10:56
 * @description：
 * @modified By：
 * @version: $
 */
public class MyConfirmListener implements ConfirmListener {

    private Map<Long, String> msgMap;

    public MyConfirmListener(Map<Long, String> msgMap) {
        this.msgMap = msgMap;
    }

    @Override
    public void handleAck(long deliveryTag, boolean multiple) throws IOException {
        System.out.println("发送的消息得到了回应");
        System.out.println("ack: deliveryTag = " + deliveryTag + " multiple: " + multiple);
    }

    @Override
    public void handleNack(long deliveryTag, boolean multiple) throws IOException {
        System.out.println("发送的消息没有得到回应");
        System.out.println("nack: deliveryTag = " + deliveryTag + " multiple: " + multiple);
        if (msgMap != null && msgMap.containsKey(deliveryTag)) {
            saveToDB(msgMap.get(deliveryTag));
        }
    }

    private void saveToDB(String msg) {
        System.out.println("消息保存到数据库成功:" + msg);
    }
}
